package tech.tongyu.bob.demo.proxy.jdkproxy.proxy;

import tech.tongyu.bob.demo.proxy.jdkproxy.service.UserService;

public class UserServiceProxy implements UserService {
    private final UserService target;

    public UserServiceProxy(UserService target) {
        this.target = target;
    }

    @Override
    public void registerUser(String username, String email) {
        try {
            System.out.println("Starting transaction...");
            // 开启事务的逻辑

            target.registerUser(username, email);  // 调用实际的业务方法

            System.out.println("Committing transaction...");
            // 提交事务的逻辑
        } catch (Exception e) {
            System.out.println("Rolling back transaction...");
            // 回滚事务的逻辑
            throw e;
        }
    }

    @Override
    public void login(String username) {
        try {
            System.out.println("Starting transaction...");
            // 开启事务的逻辑

            target.login(username);  // 调用实际的业务方法

            System.out.println("Committing transaction...");
            // 提交事务的逻辑
        } catch (Exception e) {
            System.out.println("Rolling back transaction...");
            // 回滚事务的逻辑
            throw e;
        }
    }

    @Override
    public void internalMethod() {

    }
}
